module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
  this.gameChanel = this.channelService.getChannel("GameChannel",true)
}

var handler = Handler.prototype
//获取任务信息
handler.getTaskInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.task.getTaskInfo(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}
//获取邮件列表
handler.getMailList = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.mail.getMailList(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}
//获取邮件奖励
handler.getMailAward = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	var mailId = msg.mailId
	if(!mailId){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.mail.getMailAward(null,uid,mailId,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}
//读邮件
handler.readMail = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	var mailId = msg.mailId
	if(!mailId){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.mail.readMail(null,uid,mailId,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}
//删除邮件
handler.deletedMail = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	var mailId = msg.mailId
	if(!mailId){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.mail.deletedMail(null,uid,mailId,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}
//清空已读
handler.deletedUseless = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.mail.deletedUseless(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}
//一键领取
handler.getAllMail = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.mail.getAllMail(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}